package com.softeem.score.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softeem.score.manager.mapper.MajorMapper;
import com.softeem.score.manager.service.MajorService;
import com.softeem.score.model.dto.MajorDto;
import com.softeem.score.model.entity.MajorEntity;
import com.softeem.score.model.entity.school.Major;
import com.softeem.score.model.vo.school.CourseMajorVo;
import com.softeem.score.model.vo.school.MajorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Service
public class MajorServiceImpl implements MajorService {
    @Autowired
    private MajorMapper majorMapper;

    @Transactional
    @Override
    public void updateMajor(Major major) {
        majorMapper.updateMajor(major);
    }

    @Transactional
    @Override
    public void saveMajor(Major major) {
        majorMapper.saveMajor(major);
    }

    @Override
    public List<MajorEntity> getMajorListByCollegeId(Long collegeId) {
        return majorMapper.selectMajorListByCollegeId(collegeId);
    }

    @Transactional
    @Override
    public void deleteMajorById(Long majorId) {
        // 删除当前专业所有课程
        majorMapper.deleteMajorCourseByMajorId(majorId);
        majorMapper.deleteById(majorId);
    }

    @Override
    public List<CourseMajorVo> getCourseByMajorId(Long majorId) {
        return majorMapper.getCourseByMajorId(majorId);
    }

    @Override
    public List<Long> getCheckCollege(Long majorId) {
        return majorMapper.getCheckCollegeByMajorId(majorId);
    }

    @Transactional
    @Override
    public void updateCourseMajor(CourseMajorVo courseMajor) {
        majorMapper.updateCourseMajor(courseMajor);
    }

    @Override
    public PageInfo<MajorVo> findByPage(MajorDto majorDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MajorVo> majorList = majorMapper.findByPage(majorDto);
        return new PageInfo<>(majorList);
    }

    @Transactional
    @Override
    public void saveCourseToMajor(Long majorId, List<Long> courseIdList) {
        // 找出需要添加的课程ID
        List<Long> oldCourseIds = majorMapper.selectAllByMajorId(majorId);
        List<Long> courseIdsToAdd = new ArrayList<>(courseIdList);
        courseIdsToAdd.removeAll(oldCourseIds);
        // 找出需要删除的课程ID
        List<Long> courseIdsToRemove = new ArrayList<>(oldCourseIds);
        courseIdsToRemove.removeAll(courseIdList);
        // 先删除需要删除的课程ID关系
        if (!courseIdsToRemove.isEmpty()) {
            majorMapper.deleteMajorCourseByMajorIdAndCourseIds(majorId, courseIdsToRemove);
        }
        // 再添加需要添加的课程ID关系
        if (!courseIdsToAdd.isEmpty()) {
            majorMapper.saveCourseToMajor(majorId, courseIdsToAdd);
        }
    }

}
